import { list as promotionList } from '../../../../api/promotion'
import { fill } from '../../../../api/cardBill';
import { list } from '../../../../api/vip'
const util = require('../../../../utils/util.js');
import {
	submit,
	query
} from '../../../../api/payment';
const app = getApp()
Page({
	data: {
		amount1: '',
		paymentVo: [1, 2, 3],
		balance: 0,
		point: 0,
		amount: "",
		memo: '',
		maxlength: 10,
		disabled: true,
		check: '',
		paymentType: 'weixinMiniPayPlugin',
		bankNum: '',
		bankName: '',
		bank: '',
		onlinePayment: true,
		itemAmountIndex: -1,
		vipForm: [],
		time: null,
		fastList: [],
		sn:''
	},
	onLoad: function (options) {
		this.getOnlinePayment();
		this.loadVipList()
		this.getPromotionList()
	},
	getPromotionList() {
		promotionList({ type: 12 }).then(res => {
			if (!util.isNull(res.data.data)) {
				res.data.data.map(el => {
					let flag = true
					this.data.fastList.map(fastEl => {
						if (fastEl.minimumPrice == el.minimumPrice && fastEl.amount < el.amount) {
							fastEl.amount = el.amount
							fastEl.title = el.title
							flag = false
						}
					})
					if (flag) {
						let list = {
							minimumPrice: el.minimumPrice,
							amount: el.amount,
							title: el.title
						}
						this.data.fastList.push(list)
					}
				})
				for (let i = 0; i < this.data.fastList.length - 1; i++) {
					for (let j = 0; j < this.data.fastList.length - 1 - i; j++) {
						if (this.data.fastList[j].minimumPrice > this.data.fastList[j + 1].minimumPrice) {
							let tmp = this.data.fastList[j]
							this.data.fastList[j] = this.data.fastList[j + 1];
							this.data.fastList[j + 1] = tmp;
						}
					}
				}
			}
			this.setData({
				fastList: this.data.fastList
			})
		})
	},
	getOnlinePayment() {
		this.data.onlinePayment = app.globalData.onlinePayment
		this.setData({
			onlinePayment: app.globalData.onlinePayment
		})
	},
	// getBankMessage() {
	// 	getBankInfo({ mchId: app.globalData.mchId }).then(res => {
	// 		this.data.bankNum = res.data.bankNum;
	// 		this.data.bankName = res.data.bankName;
	// 		this.data.bank = res.data.bank;
	// 		this.setData({
	// 			bankNum: this.data.bankNum,
	// 			bankName: this.data.bankName,
	// 			bank: this.data.bank
	// 		})
	// 	})
	// },
	// getBalance() {
	// 	memberInfo().then(res => {
	// 		this.data.balance = res.data.balance
	// 		this.data.point = (res.data.point / 100).toFixed(2)
	// 		this.setData({
	// 			balance: this.data.balance,
	// 			point: this.data.point
	// 		})
	// 	})
	// },
	closeInpit() {
		this.data.amount = ''
		let disabled = util.isNull(this.data.amount)
		this.setData({ amount: this.data.amount, disabled })
	},
	getInput(e) {
		let amount = e.detail.value
		const reg1 = /0*([1-9]\d*|0\.\d+)/;
		const reg2 = /(?:\.0*|(\.\d+?)0+)$/;
		amount = amount.replace(/[^\d.]/g, "") //清除“数字”和“.”以外的字符
			.replace(/\.{2,}/g, ".") //只保留第一个. 清除多余的
			.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".")
			.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') //只能输入两个小数
			.replace(reg1, '$1').replace(reg2, '$1');// 去除整数前的0和小数点后多余的0，如011.020300 => 11.0203

		let disabled = util.isNull(amount)
		this.setData({ disabled })
		clearTimeout(this.data.time)
		this.data.time = setTimeout(() => {
			if (this.data.fastList.length > 0 && amount < this.data.fastList[0].minimumPrice) {
				wx.showToast({
					icon: "none",
					title: `最低充值${this.data.fastList[0].minimumPrice}`,
				})
			}

			this.setData({ amount })
		}, 500)
	},
	getInputMemo(e) {
		let memo = e.detail.value
		this.setData({ memo })
	},
	wholeCashOut() {
		let amount = this.data.balance / 100
		this.setData({ amount, disabled: false })
	},

	choosePayMethodClick() {
		this.data.paymentType = 'weixinMiniPayPlugin'
		// this.data.paymentType = data.currentTarget.dataset.type
		this.setData({
			paymentType: this.data.paymentType
		})
		wx.showToast({
			title: '当前仅支持微信支付',
			icon: 'none'
		})
	},



	changeClick(data) {
		// this.data.paymentType = data.currentTarget.dataset.type
		// this.setData({
		// 	paymentType: this.data.paymentType
		// })
		if (data.detail.value == true) {
			this.data.paymentType = data.currentTarget.dataset.type
		} else {
			this.data.paymentType = ''
		}
		this.setData({
			paymentType: this.data.paymentType
		})

	},
	pullAmount(e) {
		const { amount } = e.currentTarget.dataset
		this.data.amount = amount
		this.paymentClick()
	},
	paymentClick() {
		if (app.globalData.token == null) {
			wx.navigateTo({
				url: '/pages/login/index',
			})
			return
		}
		var _this = this
		if (this.data.fastList.length > 0 && _this.data.amount < this.data.fastList[0].minimumPrice) {
			wx.showToast({
				icon: "none",
				title: `最低充值${this.data.fastList[0].minimumPrice}`,
			})
			return;
		}

		if (_this.data.amount <= 0 || util.isNull(_this.data.amount)) {
			wx.showToast({
				icon: "none",
				title: '请输入充值金额',
			})
			return;
		}
		if (util.isNull(_this.data.paymentType)) {
			wx.showToast({
				icon: "none",
				title: '请选择您的支付方式',
			})
			return;
		}
		fill({
			amount: _this.data.amount,
			// paymentPluginId: _this.data.paymentType
			cardType: 0,
		}).then(res => {
			if (this.data.paymentType == 'bankPayPlugin') {
				wx.showToast({
					icon: "none",
					title: '充值成功',
				})
				setTimeout(() => {
					wx.navigateBack({
						delta: 1
					})
				}, 1000)
			} else if (this.data.paymentType == 'pointPayPlugin') {
				wx.redirectTo({
					icon: "none",
					title: '充值成功',
				})
				setTimeout(() => {
					wx.navigateBack({
						delta: 1
					})
				}, 1000)
			} else if (this.data.paymentType == 'weixinMiniPayPlugin') {
				if (res.data.way != 2) {
					_this.submitClick(res.data.sn)
				} else {
					_this.data.sn = res.data.sn
					wx.showLoading({
						title: '等待支付',
					})
					wx.openEmbeddedMiniProgram({
						appId: 'wx2d00e086c8d08008',
						path: `pages/payment/payment`,
						// envVersion: 'trial',
						extraData: {
							sn: res.data.sn,
							userId: app.globalData.uuid,
							token: app.globalData.token,
							mchName: app.globalData.mchName,
							amount: res.data.amount,
							createDate: res.data.createDate,
							memo: res.data.memo,
							firstEnter: true
						},
						fail: (fail) => {
							wx.hideLoading()
							wx.showModal({
								title: '支付失败',
								content: '拉起支付失败，请点击 "允许" 跳转支付',
								showCancel: false
							})
							this.data.shippingClickFlag = true //开启防双击
						}
					})
				}
			}
		});
	},
	queryClick(data) {
		var _this = this
		query({
			sn: data
		}).then(res => {
			wx.navigateBack({
				delta: 1
			})
		}).catch(error => {
			wx.showToast({
				icon: "none",
				title: error.content,
			})
		})
	},
	submitClick(data) {
		const _this = this
		submit({
			paymentPluginId: 'weixinMiniPayPlugin',
			sn: data,
			userId: app.globalData.uuid
		}).then(res => {
			wx.hideLoading()
			if (res.type == 'success') {
				wx.requestPayment({
					'timeStamp': res.data.timeStamp,
					'nonceStr': res.data.nonceStr,
					'package': res.data.package,
					'signType': res.data.signType,
					'paySign': res.data.paySign,
					'success': function (red) {
						_this.queryClick(data)
					},
					'fail': function (red) {
						wx.showToast({
							icon: "none",
							title: '支付失败',
						})
						// wx.navigateBack({
						// 	delta: 1
						// })
					}
				})
			} else {
				wx.showToast({
					icon: "none",
					title: '支付失败',
				})
				wx.navigateBack({
					delta: 1
				})

			}
		})
	},
	chooseClick(data) {
		if (this.data.itemAmountIndex != data.currentTarget.dataset.index) {
			this.data.itemAmountIndex = data.currentTarget.dataset.index
			this.data.amount = data.currentTarget.dataset.item.amount
		} else {
			this.data.itemAmountIndex = -1
			this.data.amount = ''
		}
		this.setData({
			itemAmountIndex: this.data.itemAmountIndex,
			amount: this.data.amount
		})
	},
	loadVipList() {
		var _this = this
		_this.data.vipForm = []
		list({ mchId: app.globalData.mchId }).then(res => {
			if (res.data.data.length > 0) {
				res.data.data.forEach(function (item) {
					if (item.amount > 0 && item.vipType == 1) {
						item.vip = _this.watchType(item.vip)
						_this.data.vipForm.push(item)
					}
				})
			}
			_this.setData({
				vipForm: _this.data.vipForm
			})
		})
	},
	watchType(val) {
		if (val == 0) {
			return 'VIP0'
		} else if (val == 1) {
			return 'VIP1'
		} else if (val == 2) {
			return 'VIP2'
		} else if (val == 3) {
			return 'VIP3'
		} else if (val == 4) {
			return 'VIP4'
		} else if (val == 5) {
			return 'VIP5'
		}
	},
})


